<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.mongodb.client.MongoCursor,org.bson.Document,org.bson.types.ObjectId,java.util.Date" %>
<%@ page import="mongo.UserAccount,mongo.Department,mongo.Major,mongo.Class" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>学生信息更新</title>
<script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
	<%
		UserAccount UserAccount = new UserAccount();
    	ObjectId _id = new ObjectId(request.getParameter("_id"));
		Document result = UserAccount.getUserInfo(_id);
	  	String name = result.getString("name");
	  	String username = result.getString("username");
	  	String type = result.getString("type");
	  	String password = result.getString("passsword");
	  	String sex = result.getString("sex");
	  	String birthday = result.getString("birthday");
	  	String phone = result.getString("phone");
	  	String email = result.getString("email");
	  	String department = result.getString("department");
	  	ObjectId departmentid = result.getObjectId("departmentid");
	  	String major = result.getString("major");
		ObjectId majorid = result.getObjectId("majorid");
	  	String grade = result.getString("grade");
	  	String class1 = result.getString("class");
	  	ObjectId classid = result.getObjectId("classid");
	  	String mark = result.getString("mark");
	  	boolean isopen = result.getBoolean("isopen");
	  	//系专业班级
	  	Department Department = new Department();
		Major Major = new Major();
		Class Class = new Class();
	  	MongoCursor<Document> departmentList = Department.list();
    	MongoCursor<Document> majorList = Major.list();
    	MongoCursor<Document> classList = Class.list();
	%>
	<jsp:include page="../navbar.html" />
	<div class="container-fluid">
		<div class="row">
			<div class="col-md-1 col-md-offset-1">
				<a href="userManageServlet?method=list&type=s">
					<button type="button" class="btn btn-default"><em class="fa fa-chevron-left"></em> 返回</button>
				</a>
			</div>
		</div>
		<div class="row mt10">
			<div class="col-md-5 col-md-offset-1">
				<div class="panel">
	               	<div class="panel-body">
                 		<h4 class="page-header mt0">禁用启用</h4>
                 		<form name="teacherform" role="form" action="userManageServlet" method="post" class="form-horizontal">           			
				            <div class="form-group" style="padding: 0 20px">
					            	<span class="control-label" style="margin-right:20px">禁用/启用该用户 </span>
				                  <input name="isopen" type="checkbox" <%=isopen==true?"checked":"" %> data-size="normal" id="isopen"> 
				                  <input type="hidden" name="_id" value="<%=_id%>"> 
				                  <input type="hidden" name="type" value="s">
				                   <input type="hidden" name="open" value="<%=isopen%>">
				                  <button type="submit" class="btn btn-primary pull-right" name="method" value="changeOpen">确定</button> 			              	
			              	</div>			             	
                 		</form>                			        
			        </div>
			    </div>
			 </div>
			 <div class="col-md-5">
				<div class="panel">
	               	<div class="panel-body">
                 		<h4 class="page-header mt0">修改密码</h4>
                 		<form name="teacherform" role="form" action="userManageServlet" method="post" class="form-horizontal">           			
				            <div class="form-group" style="padding: 0 20px">
					              <span class="control-label col-md-2" style="margin-right:20px" >密码</span>
					              <div class="col-md-6">
					              	<input name="password" type="password" value="<%=result.getString("password") %>" class="form-control" readonly> 
					              </div>			               
				                  <input type="hidden" name="_id" value="<%=_id%>"> 
				                  <input type="hidden" name="type" value="s">
				                  <button type="button" class="btn btn-primary pull-right" onclick='changePwd("<%=result.getObjectId("_id") %>")'>修改</button> 			              	
			              	</div>			             	
                 		</form>                			        
			        </div>
			    </div>
			 </div>
		</div>
    	<div class="row">
    		<form name="studentform" role="form" action="userManageServlet" method="post"  onsubmit="return check(this);" class="form-horizontal">
				<div class="col-md-5 col-md-offset-1">
					<div class="panel">
	                	<div class="panel-body">
	                  		<h4 class="page-header mt0">学生信息1</h4>
	                  	    <fieldset>
				              <div class="form-group">
				                  <label class="col-md-2 control-label">姓名</label>
				                  <div class="col-md-9">
				                     <input class="form-control" type="text" name="name" value="<%=name %>" placeholder="请输入学生姓名" />
				                  </div>
				               </div> 
			                </fieldset>
		               		<fieldset>
			               		<div class="form-group">
			                  		<label class="col-md-2 control-label">学号</label>
		                  			<div class="col-md-9">
		                     			<input class="form-control" type="text" name="username" value="<%=username %>" placeholder="请输入学号" readonly />
		                  			</div>
		               			</div>	
		               		</fieldset> 
		                	<fieldset>
			               		<div class="form-group">
			               			<label class="col-md-2 control-label">性别</label>
			               			<div class="col-md-9">
			               				<label class="radio-inline c-radio">
                                 			<input id="inlineradio1" type="radio" name="sex" value="男" <%=sex.equals("男") ? "checked" : ""%>>
                                 			<span class="fa fa-circle"></span>男</label>
                        			 	<label class="radio-inline c-radio">
                           					<input id="inlineradio2" type="radio" name="sex" value="女" <%=sex.equals("女") ? "checked" : ""%>>
                           					<span class="fa fa-circle"></span>女</label>
			               			</div>
			               		</div>	
		               		</fieldset>  
		               		<fieldset>
		               			 <div class="form-group">
			                  		<label class="col-md-2 control-label">生日</label>
		                  			<div class="col-md-9">
		                     			<input class="form-control" type="date" name="birthday" value="<%=birthday %>" placeholder="请输入学生生日" />
		                  			</div>
		               			</div>
		               		</fieldset> 
		               		<fieldset>
			              		<div class="form-group">
			                  		<label class="col-md-2 control-label">手机</label>
		                  			<div class="col-md-9">
		                     			<input class="form-control" type="text" name="phone" value="<%=phone %>"  placeholder="请输入手机号" />
		                  			</div>
		               			</div>
		               		</fieldset>
		               		<fieldset>
			              		<div class="form-group">
			                  		<label class="col-md-2 control-label">邮箱</label>
		                  			<div class="col-md-9">
		                     			<input class="form-control" type="text" name="email" value="<%=email %>" placeholder="请输入邮箱" />
		                  			</div>
		               			</div>
		               		</fieldset>           
	               		</div>
	            	</div>
	         	</div>
	         	<div class="col-md-5">
					<div class="panel">
	                	<div class="panel-body">
	                  		<h4 class="page-header mt0">学生信息2</h4>	                  	
		               		<fieldset>
			               		<div class="form-group">
			                  		<label class="col-md-2 control-label">系别</label>
		                  			<div class="col-md-9">
		                  				<select class="form-control" name="departmentid" id="selectDepart">
		                  					<%while(departmentList.hasNext()) {
							                	Document document = departmentList.next();%>					                  			
							                    <option value="<%=document.getObjectId("_id") %>"  
							                    <% if(departmentid!=null){ %>               		                    
							                    <%=departmentid.equals(document.getObjectId("_id")) ? "selected" : ""%>
							                    <%} %>
							                    ><%=document.getString("name") %></option>
								             <%}%> 
		                     			</select>
		                  			</div>
		               			</div>	
		               		</fieldset> 
		                	<fieldset>
			               		<div class="form-group">
			                  		<label class="col-md-2 control-label">专业</label>
		                  			<div class="col-md-9">
		                  				<select class="form-control" name="majorid" id="selectMajor">
		                  					<option selected>请选择专业</option>
		                  					 <%	
						                  		while(majorList.hasNext()) {
						                  			Document docu = majorList.next();
						                  			if(docu.getObjectId("departmentid").equals(departmentid)) {
						                  				Document document = docu;%>
						                  				<option value="<%=document.getObjectId("_id") %>" 
							                            <% if(majorid!=null){ %>               		                    
							                            <%=majorid.equals(document.getObjectId("_id")) ? "selected" : ""%>
							                            <%} %>						                  				
						                  				 ><%=document.getString("name") %></option>
						                  		<%	}			                  		
						              
						                    } %> 
		                     			</select>
		                  			</div>
		               			</div>
		               		</fieldset>  
		               		<fieldset>
		               			 <div class="form-group">
			                  		<label class="col-md-2 control-label">年级</label>
		                  			<div class="col-md-9">                			
		                     			<input class="form-control" type="text" name="grade" value="<%=grade==null?"":grade%>" placeholder="请输入年级" />
		                  			</div>
		               			</div>
		               		</fieldset> 
		               		<fieldset>
			              		<div class="form-group">
			                  		<label class="col-md-2 control-label">班级</label>
		                  			<div class="col-md-9">
		                  				<select class="form-control" name="classid" id="selectClass">
		                  					<option selected>请选择班级</option>
		                  					 <%	
							                	 while(classList.hasNext()) {
								                 	Document docu = classList.next();
						                  			if(docu.getObjectId("majorid").equals(majorid)) {
						                  				Document document = docu;%>
						                  				<option value="<%=document.getObjectId("_id") %>"  
							                            <% if(classid!=null){ %>               		                    
							                            <%=classid.equals(document.getObjectId("_id")) ? "selected" : ""%>
							                            <%} %>							                  				
						                  				><%=document.getString("name") %></option>
						                  		<%	}	
						                    } %>
		                     			</select>
		                  			</div>
		               			</div>
		               		</fieldset> 
		               		<fieldset>
			              		<div class="form-group">
			                  		<label class="col-md-2 control-label">备注</label>
		                  			<div class="col-md-9">
		                     			<textarea class="form-control no-resize" name="mark" placeholder="请输入备注" rows="5"><%=mark %></textarea>
		                  			</div>
		               			</div>
		               		</fieldset>  
		               		<input type="hidden" name="department" id="department">
		               		<input type="hidden" name="major" id="major">
		               		<input type="hidden" name="class" id="class">
		               		<input type="hidden" name="_id" value="<%=_id%>"> 
		               		<input type="hidden" name="type" value="s" />         
	               		</div>
	            	</div>
	         	</div>
	         	<div class="col-md-10 col-md-offset-1 mb20">
          			<div class="col-md-6">
          				<button type="submit" class="btn btn-primary btn-block" name="method" value="update">确定</button> 
          			</div>
          			<div class="col-md-6">
          				<button type="reset" class="btn btn-default btn-block">重置</button> 
          			</div>
          		</div>
         	</form>
       </div>
    </div>
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						&times;
					</button>
					<h4 class="modal-title" id="myModalLabel">
						修改密码
					</h4>
				</div>
				<div class="modal-body">
				 	<form name="passwordform" role="form" action="userManageServlet" method="post"  onsubmit="return checkpassword(this);" class="form-horizontal mt20">               	  
		                <fieldset>
			            	<div class="form-group">
			            		<label class="col-md-3 control-label">新密码</label>
		                  		<div class="col-md-7">
		                     		<input class="form-control" type="password" name="newPwd" placeholder="新密码，密码长度6位以上" />
		                  		</div>
			               	</div> 
		                </fieldset>
		                <fieldset>
			            	<div class="form-group">
			            		<label class="col-md-3 control-label">确认密码</label>
		                  		<div class="col-md-7">
		                     		<input class="form-control" type="password" name="newPwd2"  placeholder="请再一次输入密码" />
		                  		</div>
			               	</div> 
		                </fieldset>
		                <input type="hidden" name="_id">
		                <input type="hidden" name="type" value="s">
		                 <div class="modal-footer">
			                <button type="button" class="btn btn-warning" data-dismiss="modal">关闭</button>
			                <button type="submit" class="btn btn-primary" name="method" value="changePwd">提交更改</button>
			            </div>	                		             	               	
		           </form>
				</div>
			</div>
		</div>
	</div>
	<script>
	$("#myModal").modal("hide");//加载页面首先隐藏模态框，指向模态框的ID
	function changePwd(_id){//a链接的参数
		$("#myModal").modal("show");//显示模态框
		document.passwordform._id.value=_id;
	}
	//表单提交前进行数据验证，数据合法则返回true表单提交
	function check(studentform) {  
		studentform.department.value =$("#selectDepart").find("option:selected").text(); 
	    studentform.major.value =$("#selectMajor").find("option:selected").text();  
	    studentform.class.value =$("#selectClass").find("option:selected").text();  
		if(studentform.name.value=="") {  
	        swal("姓名不能为空","请重新输入","warning");  
	        return false;  
	    } 
	    else if(studentform.birthday.value=="") {  
	    	swal("生日不能为空","请重新输入","warning");   
	        return false;  
	    }   
	    else if(studentform.phone.value=="") {  
	   	    swal("手机不能为空","请重新输入","warning");   
	        return false;  
	    }
	    else if(studentform.email.value=="") {  
	   	    swal("邮箱不能为空","请重新输入","warning");   
	        return false;  
	    }
	    else if(studentform.department.value=="请选择系") {
			swal("系部不能为空","请重新选择","warning");
			return false;  
		}
		else if(studentform.major.value=="请选择专业" || studentform.major.value=="目前该系没有专业") {
			swal("专业不能为空","请重新选择","warning");
			return false;  
		}
		else if(studentform.grade.value=="") {
			swal("年级不能为空","请重新选择","warning");
			return false;  
		}
		else if(studentform.class.value=="请选择班级" || studentform.class.value=="目前该专业没有班级") {
			swal("班级不能为空","请重新选择","warning");
			return false;  
		}

	    return true;  
	}   
	  
	$(document).ready(function(){
    	//系部下拉选择框变化
    	$("#selectDepart").change(function(){
    		var checkValue=$("#selectDepart").val();  
    		$.ajax({
                 type: "post",
                 url: "../../common/profileServlet?method=getmajor&_id="+checkValue,
                 dataType: "text",
                 success: function(data){
                	 var result = JSON.parse(data); 
                	 $("#selectMajor").empty(); //empty()方法删除被选元素的子元素（option）
                	 $("#selectClass").empty();                
                	 $("#selectMajor").append("<option selected>请选择专业</option>");
                	 $("#selectClass").append("<option selected>请选择班级</option>");
                	 if(result.length<1) {
                		 $("#selectMajor").append("<option>目前该系没有专业</option>");
                	 }
                	 for(var i=0; i<result.length;i++){              
                         $("#selectMajor").append("<option value='"+result[i]._id+"'>"+result[i].name+"</option>");
                	 }
                	 //$('#selectMajor option:first').attr('selected','selected');
                     console.log(result);//修改id为showMsg标签的html
                 }, error: function(){
                	 swal("请求出错","请重新选择","warning");
                 }
           })
    	});
    	
    	//专业下拉选择框变化
    	$("#selectMajor").change(function(){  
    		var checkValue=$("#selectMajor").val();  
    		$.ajax({
                 type: "post",
                 url: "../../common/profileServlet?method=getclass&_id="+checkValue,
                 dataType: "text",
                 success: function(data){
                	 var result = JSON.parse(data); 
                	 $("#selectClass").empty(); //empty()方法删除被选元素的子元素（option）
                	 if(result.length<1) {
                		 $("#selectClass").append("<option>目前该专业没有班级</option>");
                	 }
                	 for(var i=0; i<result.length;i++){              
                         $("#selectClass").append("<option value='"+result[i]._id+"'>"+result[i].name+"</option>");
                	 }
                     console.log("class:"+result);//修改id为showMsg标签的html
                 }, error: function(){
                	 swal("请求出错","请重新选择","warning");
                 }
           })
    	});
    	
    	$('[name="isopen"]').bootstrapSwitch({  
            onText:"启",  
            offText:"禁",  
            onColor:"success",   
            size:"normal", 
            onInit:function(event,state){
                state=$('[name="open"]').val();
                $("#isopen").val(state); 
            },
            onSwitchChange:function(event,state){
                if(state==true){  
                    $("#isopen").val(true);  
                }else{  
                    $("#isopen").val(false);  
                }  
            }  
        })  
  	});
	    
    function checkpassword(passwordform) {   
    	if(passwordform.newPwd.value==""||passwordform.newPwd.value.length<6) {  
	   	    swal("新密码长度不小于6","请重新输入","warning");   
	        return false;  
	    }
	    else if(passwordform.newPwd.value!=passwordform.newPwd2.value) {  
	   	    swal("两次密码输入不相同","请重新输入","warning");   
	        return false;  
	    }

	    return true;   
    }
</script>
</body>
</html>